package com.notes.java.collection;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

/**
 * @author kinson
 * @Describe java.util.Collections常用API
 * @date
 */
@Slf4j(topic = "CollectionsUtil")
public class CollectionsUtil {
    public static void main(String[] args) {
        /*List<Integer> intList = Lists.newArrayList(3, 5, 1, 6, 9);
        log.info("intList 排序前 " + intList);
        // 默认升序排序
        Collections.sort(intList);
        log.info("intList 排序后 " + intList);*/

        List<Integer> intList2 = Lists.newArrayList(3, 5, 2, 1, 3, 6, 9);
        log.info("intList2 排序前 " + intList2);
        // 指定排序规则(降序)
        /*Collections.sort(intList2, (o1, o2) -> {
            int compareTo = o1.compareTo(o2);
            if (compareTo == 1) {
                return -1;
            } else if (compareTo == -1) {
                return 1;
            }

            return compareTo;
            *//*int result = o1.compareTo(o2);
            log.info("{}.compareTo({}) == {}", o1, o2, result);
            return result;*//*
        });*/
        // log.info("intList2 排序后 " + intList2);

        /*sortASC(intList2);
        log.info("intList2 排序后 " + intList2);*/
        /*sortDESC(intList2);
        log.info("intList2 排序后 " + intList2);*/

        // aa();
        int i = 2;
        if (i == 1) {
            throw new RuntimeException("参数错误");
        }

        System.out.println("i = " + i);

        i = 2;
        System.out.println("i = " + i);
    }

    private static void sortASC(List<Integer> data) {
        Integer[] objects = data.toArray(new Integer[data.size()]);
        for (int i = 0; i < objects.length - 1; i++) {
            Integer min = objects[i];
            int minIndex = i;
            for (int j = i + 1; j < objects.length; j++) {
                Integer cur = objects[j];
                if (cur < min) {
                    min = cur;
                    minIndex = j;
                }
            }

            if (i != minIndex) {
                log.info("交换{}和{}位置，对应值为{}和{}", i, minIndex, objects[i], min);
                Integer tmp = objects[i];
                objects[i] = min;
                objects[minIndex] = tmp;
            }
        }

        ListIterator<Integer> integerListIterator = data.listIterator();
        for (Integer i : objects) {
            integerListIterator.next();
            integerListIterator.set(i);
        }
    }

    private static void sortDESC(List<Integer> data) {
        Integer[] objects = data.toArray(new Integer[data.size()]);
        for (int i = 0; i < objects.length - 1; i++) {
            Integer max = objects[i];
            int maxIndex = i;
            for (int j = i + 1; j < objects.length; j++) {
                Integer cur = objects[j];
                if (cur > max) {
                    max = cur;
                    maxIndex = j;
                }
            }

            if (i != maxIndex) {
                log.info("交换{}和{}位置，对应值为{}和{}", i, maxIndex, objects[i], max);
                Integer tmp = objects[i];
                objects[i] = max;
                objects[maxIndex] = tmp;
            }
        }

        ListIterator<Integer> integerListIterator = data.listIterator();
        for (Integer i : objects) {
            integerListIterator.next();
            integerListIterator.set(i);
        }
    }

    private static void sortBinary(List<Integer> data) {
        Integer[] objects = data.toArray(new Integer[data.size()]);
        for (int i = 0; i < objects.length - 1; i++) {
            Integer max = objects[i];
            int maxIndex = i;
            for (int j = i + 1; j < objects.length; j++) {
                Integer cur = objects[j];
                if (cur > max) {
                    max = cur;
                    maxIndex = j;
                }
            }

            if (i != maxIndex) {
                log.info("交换{}和{}位置，对应值为{}和{}", i, maxIndex, objects[i], max);
                Integer tmp = objects[i];
                objects[i] = max;
                objects[maxIndex] = tmp;
            }
        }

        ListIterator<Integer> integerListIterator = data.listIterator();
        for (Integer i : objects) {
            integerListIterator.next();
            integerListIterator.set(i);
        }
    }

    public static void aa () {
        //初始化数组
        int[] array = {20,40,90,30,80,70,50};

        //排序
        int j;
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            for (j = i - 1; j > 0 && array[j] > temp; j--) {
                array[j+1] = array[j];
            }
            if (i != j + 1) {
                array[j+1] = temp;
            }
        }

        //输出
        System.out.println(Arrays.toString(array));
    }
}


